<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      v-show="showSearch"
      :inline="true"
      @submit.native.prevent
    >
      <el-form-item label="考勤组名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入考勤组名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="班次状态"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in selectStatusOptions"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['job:termAttendanceGroup:add']"
          >新增
        </el-button>
        <el-button
          type="primary"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleDelete"
          v-hasPermi="['job:termAttendanceGroup:remove']"
          >删除
        </el-button>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleRules"
          v-hasPermi="['job:termAttendanceGroup:set']"
          >考勤规则设置
        </el-button>
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="pageInit"
      ></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      :data="timeShiftList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{ (pageNum - 1) * pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="考勤组名称"
        prop="name"
        :show-overflow-tooltip="true"
        width="100"
      />
      <el-table-column label="对应班次" prop="shiftName" width="150" />
      <!-- <el-table-column label="上下班时间" prop="showDateTime" width="200" /> -->
      <el-table-column label="上下班时间" prop="signItems" min-width="200">
        <template slot-scope="scope">
          <pre>{{ scope.row.signItems | signItemsFilter }}</pre>
        </template>
      </el-table-column>
      <el-table-column
        label="创建人员"
        prop="createBy"
        :show-overflow-tooltip="true"
        width="120"
      />
      <el-table-column label="创建时间" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="状态" align="center" key="status">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="0"
            :inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['job:termAttendanceGroup:edit']"
            >修改
          </el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['job:termAttendanceGroup:view']"
            >详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      v-show="total > 10"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      :page-sizes="[10]"
      @pagination="pageInit"
    />
    <!-- 添加或修改班次对话框 -->
    <!-- <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :key="formKey" label-width="80px">
        <span style="font-weight:bold">基本信息</span>
        <el-divider></el-divider>
        <el-row>
          <el-col :span="12">
            <el-form-item label="考勤组名称" prop="name" label-width="120px">
              <el-input v-model="form.name" placeholder="请输入考勤组名称" style="width: 200px"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="对应班次" prop="shiftId">
              <el-select v-model="form.shiftId"  placeholder="请选择">
                <el-option
                  v-for="item in timeShiftOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"

                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in statusOptions"
                  :key="dict.label"
                  :label="dict.value"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <span>
         <span style="font-weight:bold">打卡时段设置</span>
          <span>（该班次为一天上下班打卡次数）</span>
        </span>

        <el-divider></el-divider>
        <el-row>
          <el-tag type="info" :color="color1" @click="changTag(1)">一天1次上下班打卡</el-tag>
          <el-tag type="info" :color="color2" @click="changTag(2)">一天2次上下班打卡</el-tag>
          <el-tag type="info" :color="color3" @click="changTag(3)">一天3次上下班打卡</el-tag>
          <el-tag type="info" :color="color4" @click="changTag(4)">一天4次上下班打卡</el-tag>
        </el-row>

        <el-row style="margin-top: 15px">
          <el-col :span="12">
            <span width="60px">上下班时间</span>
          </el-col>
        </el-row>


        <el-row style="margin-top: 15px">
          <el-col :span="12">
            <el-form-item label="第1次 上班" prop="firstBeginTime" v-show="showTime1">
              <el-time-select
                v-model="form.firstBeginTime"

                clearable
                :picker-options="{
                start: '00:00',
              step: '00:15',
              end: '23:59'
            }"
                placeholder="">
              </el-time-select>
              <el-checkbox v-model="form.firstBeginChecked" style="margin-left: 8px">次日</el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="第1次 下班" prop="firstEndTime" v-show="showTime1">
              <el-time-select
                v-model="form.firstEndTime"
                clearable

                :picker-options="{
                  start: '00:00',
                  step: '00:15',
                  end: '23:59'

            }"
                placeholder="">
              </el-time-select>
              <el-checkbox v-model="form.firstEndChecked" style="margin-left: 8px">次日</el-checkbox>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="第2次 上班" prop="secondBeginTime" v-show="showTime2">
              <el-time-select
                v-model="form.secondBeginTime"
                clearable
                :picker-options="{
                 start: '00:00',
                step: '00:15',
                end: '23:59'

            }"
                placeholder="">
              </el-time-select>
              <el-checkbox v-model="form.secondBeginChecked" style="margin-left: 8px">次日</el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="第2次 下班" prop="secondEndTime" v-show="showTime2">
              <el-time-select
                v-model="form.secondEndTime"
                clearable
                :picker-options="{
                   start: '00:00',
                    step: '00:15',
                    end: '23:59'

            }"
                placeholder="">
              </el-time-select>
              <el-checkbox v-model="form.secondEndChecked" style="margin-left: 8px">次日</el-checkbox>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="第3次 上班" prop="threeBeginTime" v-show="showTime3">
              <el-time-select
                v-model="form.threeBeginTime"
                clearable
                :picker-options="{
                  start: '00:00',
                  step: '00:15',
                  end: '23:59'

            }"
                placeholder="">
              </el-time-select>
              <el-checkbox v-model="form.threeBeginChecked" style="margin-left: 8px">次日</el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="第3次 下班" prop="threeEndTime" v-show="showTime3">
              <el-time-select
                v-model="form.threeEndTime"
                clearable
                :picker-options="{
                 start: '00:00',
                  step: '00:15',
                  end: '23:59'

            }"
                placeholder="">
              </el-time-select>
              <el-checkbox v-model="form.threeEndChecked" style="margin-left: 8px">次日</el-checkbox>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="第4次 上班" prop="fourBeginTime" v-show="showTime4">
              <el-time-select
                v-model="form.fourBeginTime"
                clearable
                :picker-options="{
                start: '00:00',
                step: '00:15',
                end: '23:59'


            }"
                placeholder="">
              </el-time-select>
              <el-checkbox v-model="form.fourBeginChecked" style="margin-left: 8px">次日</el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="第4次 下班" prop="fourEndTime" v-show="showTime4">
              <el-time-select
                v-model="form.fourEndTime"
                clearable
                :picker-options="{
                start: '00:00',
                step: '00:15',
               end: '23:59'
            }"
                placeholder=" ">
              </el-time-select>
              <el-checkbox v-model="form.fourEndChecked" style="margin-left: 8px">次日</el-checkbox>
            </el-form-item>
          </el-col>

        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog> -->

    <!-- 表单 -->
    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <handle-form
        :statusOptions="statusOptions"
        :timeShiftOptions="timeShiftOptions"
        :loading="formLoading"
        :flag="flag"
        :initData="initData"
        @cancel="cancel"
        @confirm="formSubmit"
      />
    </el-dialog>
    <!-- 详情对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="openView"
      width="780px"
      append-to-body
    >
      <el-form ref="formView" :model="formView" label-width="80px">
        <span style="font-weight:bold">基本信息</span>
        <el-divider></el-divider>
        <el-row>
          <el-col :span="12">
            <el-form-item label="考勤组名称" prop="name" label-width="120px">
              <el-input
                v-model="formView.name"
                placeholder="请输入考勤组名称"
                disabled
                style="width: 180px"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="对应班次" prop="shiftId">
              <el-select v-model="formView.shiftId" placeholder="请选择">
                <el-option
                  v-for="item in timeShiftOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                  disabled
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="formView.status">
                <el-radio
                  v-for="dict in statusOptions"
                  :key="dict.label"
                  :label="dict.value"
                  >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <span>
          <span style="font-weight:bold">打卡时段设置</span>
          <span>（该班次为一天上下班打卡次数）</span>
        </span>

        <el-divider></el-divider>
        <el-row>
          <el-tag type="info" :color="color1">一天1次上下班打卡</el-tag>
          <el-tag type="info" :color="color2">一天2次上下班打卡</el-tag>
          <el-tag type="info" :color="color3">一天3次上下班打卡</el-tag>
          <el-tag type="info" :color="color4">一天4次上下班打卡</el-tag>
        </el-row>

        <el-row style="margin-top: 15px">
          <el-col :span="12">
            <span width="60px">上下班时间</span>
          </el-col>
        </el-row>

        <el-row style="margin-top: 15px">
          <el-col :span="12">
            <el-form-item
              label="第1次 上班"
              prop="firstBeginTime"
              v-show="showTime1"
            >
              <el-time-select
                v-model="formView.firstBeginTime"
                disabled
                :picker-options="{
                  start: '00:00',
                  step: '00:15',
                  end: '23:59'
                }"
                placeholder=""
              >
              </el-time-select>
              <el-checkbox
                v-model="formView.firstBeginChecked"
                style="margin-left: 8px"
                disabled
                >次日</el-checkbox
              >
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="第1次 下班"
              prop="firstEndTime"
              v-show="showTime1"
            >
              <el-time-select
                v-model="formView.firstEndTime"
                disabled
                :picker-options="{
                  start: '00:00',
                  step: '00:15',
                  end: '23:59'
                }"
                placeholder=""
              >
              </el-time-select>
              <el-checkbox
                v-model="formView.firstEndChecked"
                style="margin-left: 8px"
                disabled
                >次日</el-checkbox
              >
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="第2次 上班"
              prop="secondBeginTime"
              v-show="showTime2"
              disabled
            >
              <el-time-select
                v-model="formView.secondBeginTime"
                disabled
                :picker-options="{
                  start: '00:00',
                  step: '00:15',
                  end: '23:59'
                }"
                placeholder=""
              >
              </el-time-select>
              <el-checkbox
                v-model="formView.secondBeginChecked"
                style="margin-left: 8px"
                disabled
                >次日</el-checkbox
              >
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="第2次 下班"
              prop="secondEndTime"
              v-show="showTime2"
              disabled
            >
              <el-time-select
                v-model="formView.secondEndTime"
                disabled
                :picker-options="{
                  start: '00:00',
                  step: '00:15',
                  end: '23:59'
                }"
                placeholder=""
              >
              </el-time-select>
              <el-checkbox
                v-model="formView.secondEndChecked"
                style="margin-left: 8px"
                disabled
                >次日</el-checkbox
              >
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="第3次 上班"
              prop="threeBeginTime"
              v-show="showTime3"
            >
              <el-time-select
                disabled
                v-model="formView.threeBeginTime"
                :picker-options="{
                  start: '00:00',
                  step: '00:15',
                  end: '23:59'
                }"
                placeholder=""
              >
              </el-time-select>
              <el-checkbox
                v-model="formView.threeBeginChecked"
                style="margin-left: 8px"
                disabled
                >次日</el-checkbox
              >
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="第3次 下班"
              prop="threeEndTime"
              v-show="showTime3"
            >
              <el-time-select
                v-model="formView.threeEndTime"
                disabled
                :picker-options="{
                  start: '00:00',
                  step: '00:15',
                  end: '23:59'
                }"
                placeholder=""
              >
              </el-time-select>
              <el-checkbox
                v-model="formView.threeEndChecked"
                style="margin-left: 8px"
                disabled
                >次日</el-checkbox
              >
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="第4次 上班"
              prop="fourBeginTime"
              v-show="showTime4"
            >
              <el-time-select
                v-model="formView.fourBeginTime"
                disabled
                :picker-options="{
                  start: '00:00',
                  step: '00:15',
                  end: '23:59'
                }"
                placeholder=""
              >
              </el-time-select>
              <el-checkbox
                v-model="formView.fourBeginChecked"
                style="margin-left: 8px"
                disabled
                >次日</el-checkbox
              >
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="第4次 下班"
              prop="fourEndTime"
              v-show="showTime4"
              disabled
            >
              <el-time-select
                v-model="formView.fourEndTime"
                disabled
                :picker-options="{
                  start: '00:00',
                  step: '00:15',
                  end: '23:59'
                }"
                placeholder=" "
              >
              </el-time-select>
              <el-checkbox
                v-model="formView.fourEndChecked"
                style="margin-left: 8px"
                disabled
                >次日</el-checkbox
              >
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :title="title"
      :visible.sync="openSet"
      width="780px"
      append-to-body
    >
      <rule-form
        :loading="formLoading"
        :initData="ruleRecords"
        @confirm="ruleSubmit"
        @cancel="cancel"
      />
    </el-dialog>

    <!-- <el-dialog
      :title="title"
      :visible.sync="openSet"
      width="780px"
      append-to-body
    >
      <el-form
        ref="formSet"
        :model="formSet"
        :rules="rulesSet"
        label-width="80px"
      >
        <el-row>
          1、迟到状态标准：晚于上班打卡
          <el-input
            v-model="formSet.late"
            placeholder=" "
            size="small"
            style="width: 60px"
          />
          分钟内计为迟到；晚于上班打卡

          <el-input
            v-model="formSet.generallyLate"
            placeholder=" "
            size="small"
            style="width: 60px"
          />
          分钟内计为一般迟到；晚于上班打卡
          <el-input
            v-model="formSet.seriouslyLate"
            placeholder=" "
            size="small"
            style="width: 60px"
          />
          分钟内计为严重迟到；
        </el-row>
        <el-divider></el-divider>
        <el-row>
          2、早退状态标准：早于下班打卡
          <el-input
            v-model="formSet.leaveEarly"
            placeholder=""
            size="small"
            style="width: 60px"
          />
          分钟内计为早退；
        </el-row>
        <el-divider></el-divider>
        <el-row>
          3、旷工状态标准：迟到超过
          <el-input
            v-model="formSet.absenteeism"
            placeholder=" "
            size="small"
            style="width: 60px"
          />
          分钟计为该班次旷工，超过早退时间也为该班次旷工；
        </el-row>
        <el-divider></el-divider>
        <el-row style="margin-left:500px">
          <el-button type="primary" @click="submitSetForm">保 存</el-button>
          <el-button @click="cancel">取 消</el-button>
        </el-row>
      </el-form>
    </el-dialog> -->

    <!-- <el-dialog :title="title" :visible.sync="openSet" width="780px" append-to-body>
      崩瞎卡拉卡
    </el-dialog> -->
  </div>
</template>

<script>
// 接口
import {
  listTermAttendanceGroup,
  add,
  getTermAttendanceGroup,
  delTermAttendanceGroup,
  update,
  modifTermAttendanceGroupStatus,
  listTimeShiftList,
  getTermAttendanceRules,
  addTermAttendanceRules
} from "@/api/job/termAttendanceGroup/termAttendanceGroup";
// v1.2接口
import {
  addNewOne,
  pageData,
  multiDelete,
  updateV2,
  changeStatus,
  addOrUpdateRules,
  ruleRecords
} from "@/api/job/termAttendanceGroup/termAttendanceGroup_v2";
// 组件
import HandleForm from "./component/Form.vue"; // 班次表单
import RuleForm from "./component/RuleForm.vue"; // 规则表单

export default {
  name: "termAttendanceGroupIndex",
  components: {
    HandleForm,
    RuleForm
  },
  data() {
    return {
      show: false,
      formKey: 1,
      type: 4,
      showEdit: [],
      needNumber: 0,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      ids: [],
      statusArray: [],
      flag: false,
      loading: true,
      formLoading: false,
      initData: {}, // 详情表单初始化数据
      ruleRecords: [],
      // 总条数
      total: 0,
      pageNum: 1,
      pageSize: 10,
      // 显示搜索条件
      showSearch: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      openView: false,
      // 是否显示弹出层
      openSet: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        status: ""
      },
      // 表单参数
      form: { status: 0 },
      formView: {},
      formSet: {},
      priceFormOpen: false,
      priceForm: {
        // 是否显示弹出层（用户导入）
        // 弹出层标题（用户导入）
        title: "修改价格"
      },
      color0: "",
      color1: "",
      color2: "",
      color3: "",
      color4: "#000000",
      showTime1: true,
      showTime2: true,
      showTime3: true,
      showTime4: true,
      showDiv: true,
      timeShiftList: [],
      timeShiftOptions: [],
      statusOptions: [
        { value: 0, label: "启用" },
        { value: 1, label: "停用" }
      ],
      selectStatusOptions: [
        { value: -1, label: "请选择" },
        { value: 0, label: "启用" },
        { value: 1, label: "停用" }
      ],
      rules: {
        name: [
          { required: true, message: "考勤组名称不能为空", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "考勤组名称长度必须介于 2 和 20 之间",
            trigger: "blur"
          }
        ],
        status: [{ required: true, message: "状态不能空" }]
      },

      rulesSet: {
        late: [
          {
            required: true,
            type: "number",
            message: "迟到不能为空且需为数字",
            trigger: "blur",
            transform: value => Number(value)
          }
        ],
        generallyLate: [
          {
            required: true,
            type: "number",
            message: "一般迟到不能为空且需为数字",
            trigger: "blur",
            transform: value => Number(value)
          }
        ],
        seriouslyLate: [
          {
            required: true,
            type: "number",
            message: "严重迟到不能为空且需为数字",
            trigger: "blur",
            transform: value => Number(value)
          }
        ],
        leaveEarly: [
          {
            required: true,
            type: "number",
            message: "早退不能为空且需为数字",
            trigger: "blur",
            transform: value => Number(value)
          }
        ],
        absenteeism: [
          {
            required: true,
            type: "number",
            message: "旷工不能为空且需为数字",
            trigger: "blur",
            transform: value => Number(value)
          }
        ]
      }
    };
  },
  filters: {
    signItemsFilter(v) {
      const data = JSON.parse(v);
      let result = "";
      data.forEach(item => {
        const typeName = item.type == 1 ? "上班" : "下班";
        const isNextDay = item.nextDay ? "次日" : "";
        result += `第${
          item.sortNum
        }次${typeName}:${isNextDay}${item.startTime || "无"}-${item.endTime ||
          "无"}\r\n`;
      });
      return result;
    }
  },
  created() {
    // this.getList();
    this.pageInit();
    this.listTimeShiftList();
  },
  methods: {
    // 页面初始化
    pageInit() {
      this.loading = true;
      if (this.queryParams.status === "") {
        this.queryParams.status = -1;
      }
      pageData(this.addDateRange(this.queryParams, this.dateRange)).then(
        res => {
          this.timeShiftList = res.rows;
          this.total = res.total;
          this.loading = false;
        }
      );
    },
    // 新增规则表单提交
    ruleSubmit(form) {
      this.formLoading = true;
      addOrUpdateRules(form).then(res => {
        if (res.code == 200) {
          this.$message.success("操作成功");
        }
        this.formLoading = false;
        this.cancel();
      });
    },
    // 表单提交
    formSubmit(form) {
      this.formLoading = true;
      if (form.id) {
        updateV2(form).then(res => {
          this.$message.success(res.msg);
          this.formLoading = false;
          this.open = false;
          this.pageInit();
        });
      } else {
        addNewOne(form).then(res => {
          this.$message.success(res.msg);
          this.formLoading = false;
          this.open = false;
          this.pageInit();
        });
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.statusArray = selection.map(item => item.status);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.openView = false;
      this.openSet = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        type: undefined,
        status: undefined,
        firstBeginTime: "",
        firstEndTime: "",
        secondBeginTime: "",
        secondEndTime: "",
        threeBeginTime: "",
        threeEndTime: "",
        fourBeginTime: "",
        fourEndTime: "",
        firstBeginChecked: false,
        firstEndChecked: false,
        secondBeginChecked: false,
        secondEndChecked: false,
        threeBeginChecked: false,
        threeEndChecked: false,
        fourBeginChecked: false,
        fourEndChecked: false
      };
      this.resetForm("form");
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      this.reset();

      listTermAttendanceGroup(
        this.addDateRange(this.queryParams, this.dateRange)
      ).then(response => {
        this.timeShiftList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /* handleStatusChange(row) {
      let text = row.status == 0 ? "启用" : "停用";

      this.$confirm("确认要" + text + row.name + '"班次吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          let data = { id: row.id, status: row.status };
          return modifTermAttendanceGroupStatus(data);
        })
        .then(() => {
          this.msgSuccess(text + "成功");
        })
        .catch(function() {
          row.status = row.status == 0 ? 1 : 0;
        });
    }, */

    //v1.2状态变更
    handleStatusChange(row) {
      let text = row.status == 0 ? "启用" : "停用";

      this.$confirm("确认要" + text + row.name + '"班次吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          let data = { id: row.id, status: row.status };
          return changeStatus(data);
        })
        .then(() => {
          this.msgSuccess(text + "成功");
        })
        .catch(function() {
          row.status = row.status == 0 ? 1 : 0;
        });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.pageInit();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 删除按钮操作 */
    /* handleDelete(row) {
      const id = row.id || this.ids;
      const status = row.status || this.statusArray;
      if (id == "") {
        this.$alert("请先选中记录后再删除！", "错误提示", {
          dangerouslyUseHTMLString: true
        });
        return;
      }
      if (this.statusArray.length > 0) {
        for (var i = 0; i < status.length; i++) {
          if (status[i] == 0) {
            this.$alert("有启用状态不能删除！", "错误提示", {
              dangerouslyUseHTMLString: true
            });
            return;
          }
        }
      } else if (row.status == 0) {
        this.$alert("有启用状态不能删除！", "错误提示", {
          dangerouslyUseHTMLString: true
        });
        return;
      }
      this.$confirm('是否确认删除编号为"' + id + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delTermAttendanceGroup(id);
        })
        .then(() => {
          // this.getList();
          this.pageInit()
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    }, */
    // v1.2删除修改
    handleDelete(row) {
      const id = row.id || this.ids;
      const status = row.status || this.statusArray;
      if (id == "") {
        this.$message.error("请先选中记录后再删除！");
        return;
      }
      if (this.statusArray.length > 0) {
        for (var i = 0; i < status.length; i++) {
          if (status[i] == 0) {
            this.$message.error("有启用状态不能删除！");
            return;
          }
        }
      } else if (row.status == 0) {
        if (status[i] == 0) {
          this.$message.error("有启用状态不能删除！");
          return;
        }
        return;
      }
      this.$confirm('是否确认删除编号为"' + id + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return multiDelete(id);
        })
        .then(() => {
          // this.getList();
          this.pageInit();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 提交按钮 */
    submitForm: function() {
      if (this.type == 1) {
        if (
          this.form.firstBeginTime == null ||
          this.form.firstEndTime == null
        ) {
          this.$alert("第一次上下班打卡中有空值，请核对", "错误提示", {
            dangerouslyUseHTMLString: true
          });
          return;
        }
      } else if (this.type == 2) {
        if (
          this.form.firstBeginTime == null ||
          this.form.firstEndTime == null
        ) {
          this.$alert("第一次上下班打卡中有空值，请核对", "错误提示", {
            dangerouslyUseHTMLString: true
          });
          return;
        }
        if (
          this.form.secondBeginTime == null ||
          this.form.secondEndTime == null
        ) {
          this.$alert("第二次上下班打卡中有空值，请核对", "错误提示", {
            dangerouslyUseHTMLString: true
          });
          return;
        }
      } else if (this.type == 3) {
        if (
          this.form.firstBeginTime == null ||
          this.form.firstEndTime == null
        ) {
          this.$alert("第一次上下班打卡中有空值，请核对", "错误提示", {
            dangerouslyUseHTMLString: true
          });
          return;
        }
        if (
          this.form.secondBeginTime == null ||
          this.form.secondEndTime == null
        ) {
          this.$alert("第二次上下班打卡中有空值，请核对", "错误提示", {
            dangerouslyUseHTMLString: true
          });
          return;
        }
        if (
          this.form.threeBeginTime == null ||
          this.form.threeEndTime == null
        ) {
          this.$alert("第三次上下班打卡中有空值，请核对", "错误提示", {
            dangerouslyUseHTMLString: true
          });
          return;
        }
      } else if (this.type == 4) {
        if (
          this.form.firstBeginTime == null ||
          this.form.firstEndTime == null
        ) {
          this.$alert("第一次上下班打卡中有空值，请核对", "错误提示", {
            dangerouslyUseHTMLString: true
          });
          return;
        }
        if (
          this.form.secondBeginTime == null ||
          this.form.secondEndTime == null
        ) {
          this.$alert("第二次上下班打卡中有空值，请核对", "错误提示", {
            dangerouslyUseHTMLString: true
          });
          return;
        }
        if (
          this.form.threeBeginTime == null ||
          this.form.threeEndTime == null
        ) {
          this.$alert("第三次上下班打卡中有空值，请核对", "错误提示", {
            dangerouslyUseHTMLString: true
          });
          return;
        }
        if (this.form.fourBeginTime == null || this.form.fourEndTime == null) {
          this.$alert("第四次上下班打卡中有空值，请核对", "错误提示", {
            dangerouslyUseHTMLString: true
          });
          return;
        }
      }

      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.firstBeginChecked) {
            this.form.firstBeginChecked = 1;
          } else {
            this.form.firstBeginChecked = 0;
          }
          if (this.form.firstEndChecked) {
            this.form.firstEndChecked = 1;
          } else {
            this.form.firstEndChecked = 0;
          }
          if (this.form.secondBeginChecked) {
            this.form.secondBeginChecked = 1;
          } else {
            this.form.secondBeginChecked = 0;
          }
          if (this.form.secondEndChecked) {
            this.form.secondEndChecked = 1;
          } else {
            this.form.secondEndChecked = 0;
          }
          if (this.form.threeBeginChecked) {
            this.form.threeBeginChecked = 1;
          } else {
            this.form.threeBeginChecked = 0;
          }
          if (this.form.threeEndChecked) {
            this.form.threeEndChecked = 1;
          } else {
            this.form.threeEndChecked = 0;
          }
          if (this.form.fourBeginChecked) {
            this.form.fourBeginChecked = 1;
          } else {
            this.form.fourBeginChecked = 0;
          }
          if (this.form.fourEndChecked) {
            this.form.fourEndChecked = 1;
          } else {
            this.form.fourEndChecked = 0;
          }

          this.form.type = this.type;

          if (this.form.id != undefined) {
            update(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              // this.getList();
              this.pageInit();
            });
          } else {
            add(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              // this.getList();
              this.pageInit();
            });
          }
        }
      });
    },

    submitSetForm() {
      if (!Number(this.formSet.late) || Number(this.formSet.late) < 0) {
        this.$alert("迟到时间值为数字且大于0", "错误提示", {
          dangerouslyUseHTMLString: true
        });
        return;
      }
      if (
        !Number(this.formSet.generallyLate) ||
        Number(this.formSet.generallyLate) < 0
      ) {
        this.$alert("一般迟到时间值为数字且大于0", "错误提示", {
          dangerouslyUseHTMLString: true
        });
        return;
      }
      if (
        !Number(this.formSet.seriouslyLate) ||
        Number(this.formSet.seriouslyLate) < 0
      ) {
        this.$alert("严重迟到时间值为数字且大于0", "错误提示", {
          dangerouslyUseHTMLString: true
        });
        return;
      }
      if (
        !Number(this.formSet.leaveEarly) ||
        Number(this.formSet.leaveEarly) < 0
      ) {
        this.$alert("早退时间值为数字且大于0", "错误提示", {
          dangerouslyUseHTMLString: true
        });
        return;
      }
      if (
        !Number(this.formSet.absenteeism) ||
        Number(this.formSet.absenteeism) < 0
      ) {
        this.$alert("旷工时间值为数字且大于0", "错误提示", {
          dangerouslyUseHTMLString: true
        });
        return;
      }
      if (Number(this.formSet.late) > Number(this.formSet.generallyLate)) {
        this.$alert(
          "必须遵守普通迟到时间<一般迟到时间<严重迟到时间<旷工时间的规则,请核对",
          "错误提示",
          { dangerouslyUseHTMLString: true }
        );
        return;
      }
      if (
        Number(this.formSet.generallyLate) > Number(this.formSet.seriouslyLate)
      ) {
        this.$alert(
          "必须遵守普通迟到时间<一般迟到时间<严重迟到时间<旷工时间的规则,请核对",
          "错误提示",
          { dangerouslyUseHTMLString: true }
        );
        return;
      }
      if (
        Number(this.formSet.seriouslyLate) > Number(this.formSet.absenteeism)
      ) {
        this.$alert(
          "必须遵守普通迟到时间<一般迟到时间<严重迟到时间<旷工时间的规则,请核对",
          "错误提示",
          { dangerouslyUseHTMLString: true }
        );
        return;
      }
      this.$refs["formSet"].validate(valid => {
        if (valid) {
          addTermAttendanceRules(this.formSet).then(response => {
            this.msgSuccess("设置成功");
            this.openSet = false;
          });
        }
      });
    },
    /* handleAdd() {
      this.reset();
      this.formKey++;
      this.open = true;
      this.title = "考勤组信息";
    }, */
    handleAdd() {
      this.title = "考勤组信息";
      this.flag = !this.flag;
      this.open = true;
    },
    /** 修改按钮操作 */
    /* handleUpdate(row) {
      this.reset();
      this.formKey++;
      const id = row.id || this.ids;
      if (row.status == 0) {
        this.$alert("启用状态不能修改！", "错误提示", {
          dangerouslyUseHTMLString: true
        });
        return;
      }

      getTermAttendanceGroup(id).then(response => {
        this.form = response.data;

        if (this.form.firstBeginChecked == 1) {
          this.form.firstBeginChecked = true;
        }
        if (this.form.firstEndChecked == 1) {
          this.form.firstEndChecked = true;
        }
        if (this.form.secondBeginChecked == 1) {
          this.form.secondBeginChecked = true;
        }
        if (this.form.secondEndChecked) {
          this.form.secondEndChecked = true;
        }
        if (this.form.threeBeginChecked) {
          this.form.threeBeginChecked = true;
        }
        if (this.form.threeEndChecked) {
          this.form.threeEndChecked = true;
        }
        if (this.form.fourBeginChecked) {
          this.form.fourBeginChecked = true;
        }
        if (this.form.fourEndChecked) {
          this.form.fourEndChecked = true;
        }

        this.changTag(this.form.type);
        this.open = true;
        this.title = "考勤组信息";
      });
    }, */

    handleUpdate(row) {
      this.initData = Object.assign({}, row);
      this.initData["signItems"] = JSON.parse(row.signItems);
      this.initData["disabled"] = false;
      this.open = true;
    },
    /* handleView(row) {
      this.reset();
      const id = row.id || this.ids;
      getTermAttendanceGroup(id).then(response => {
        this.formView = response.data;
        if (this.formView.firstBeginChecked == 1) {
          this.formView.firstBeginChecked = true;
        }
        if (this.formView.firstEndChecked == 1) {
          this.formView.firstEndChecked = true;
        }
        if (this.formView.secondBeginChecked == 1) {
          this.formView.secondBeginChecked = true;
        }
        if (this.formView.secondEndChecked) {
          this.formView.secondEndChecked = true;
        }
        if (this.formView.threeBeginChecked) {
          this.formView.threeBeginChecked = true;
        }
        if (this.formView.threeEndChecked) {
          this.formView.threeEndChecked = true;
        }
        if (this.formView.fourBeginChecked) {
          this.formView.fourBeginChecked = true;
        }
        if (this.formView.fourEndChecked) {
          this.formView.fourEndChecked = true;
        }
        this.changTag(this.formView.type);
        this.openView = true;
        this.title = "考勤组信息";
      });
    }, */
    // v1.2详情
    handleView(row) {
      this.initData = Object.assign({}, row);
      this.initData["signItems"] = JSON.parse(row.signItems);
      this.initData["disabled"] = true;
      this.open = true;
    },
    changTag(value) {
      if (value == 0) {
        this.color0 = "#000000";
        this.color1 = "0";
        this.color2 = "";
        this.color3 = "";
        this.color4 = "";
        this.showDiv = false;
        this.showTime1 = false;
        this.showTime2 = false;
        this.showTime3 = false;
        this.showTime4 = false;
        this.form.secondBeginTime = "";
        this.form.secondEndTime = "";
        this.form.threeBeginTime = "";
        this.form.threeEndTime = "";
        this.form.fourBeginTime = "";
        this.form.fourEndTime = "";
        this.type = 0;
      } else if (value == 1) {
        this.color0 = "";
        this.color1 = "#000000";
        this.color2 = "";
        this.color3 = "";
        this.color4 = "";
        this.showDiv = true;
        this.showTime1 = true;
        this.showTime2 = false;
        this.showTime3 = false;
        this.showTime4 = false;
        this.form.secondBeginTime = "";
        this.form.secondEndTime = "";
        this.form.threeBeginTime = "";
        this.form.threeEndTime = "";
        this.form.fourBeginTime = "";
        this.form.fourEndTime = "";
        this.type = 1;
      } else if (value == 2) {
        this.color0 = "";
        this.color2 = "#000000";
        this.color1 = "";
        this.color3 = "";
        this.color4 = "";
        this.showTime1 = true;
        this.showTime2 = true;
        this.showTime3 = false;
        this.showTime4 = false;
        this.form.threeBeginTime = "";
        this.form.threeEndTime = "";
        this.form.fourBeginTime = "";
        this.form.fourEndTime = "";
        this.type = 2;
      } else if (value == 3) {
        this.color0 = "";
        this.color3 = "#000000";
        this.color2 = "";
        this.color1 = "";
        this.color4 = "";
        this.showDiv = true;
        this.showTime1 = true;
        this.showTime2 = true;
        this.showTime3 = true;
        this.showTime4 = false;

        this.form.fourBeginTime = "";
        this.form.fourEndTime = "";
        this.type = 3;
      } else if (value == 4) {
        this.color0 = "";
        this.color1 = "";
        this.color2 = "";
        this.color3 = "";
        this.color4 = "#000000";
        this.showDiv = true;
        this.showTime1 = true;
        this.showTime2 = true;
        this.showTime3 = true;
        this.showTime4 = true;
        this.type = 4;
      }
    },

    // v1.0
    /* handleRules() {
      this.reset();
      getTermAttendanceRules().then(response => {
        if (response.data != null) {
          this.formSet = response.data;
        }

        this.openSet = true;
        this.title = "考勤规则设置";
      });
    }, */

    // v1.2
    handleRules() {
      ruleRecords().then(res => {
        this.ruleRecords = res.data;
        this.title = "考勤规则设置";
        this.openSet = true;
      });
    },

    listTimeShiftList() {
      this.loading = true;
      listTimeShiftList({status:1}).then(response => {
        this.timeShiftOptions = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    }
  }
};
</script>

<style scoped></style>
